<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Term_m extends Base_model
{
	public $primary_key = 'term_id';
	protected $soft_delete = TRUE;
	public $_table = "term";
	public $has_many = array('term_posts' => array('primary_key' => 'term_id'));
	
	function key_value($data, $value, $key = '') {
		if (!$key) {
			$key = $this->primary_key;
		}
		$result = array();
		if ($data) foreach ($data as $item) {
			if (is_array($item)) $item = (object)$item;
			$result[$item->$key] = $item->$value;
		}
		return $result;
	}
	function get_parent($parent = 0, $status = 1) {
		$this->db->order_by('term_order asc');
		if ($status >= 0) {
			$this->db->where('term_status', $status);
		}
		return $this->get_many_by(array('term_parent' => $parent,));
	}
	public function get_menu($parent = 0, $type = '') {
		$this->order_by('term_parent asc');
		$this->order_by('term_order asc');
		if ($type != '') {
			$this->where('term_type', $type);
		}
		$this->where('term_status >', 0);
		$all_menu = $this->get_many_by();
		$result = $this->get_recursive($all_menu, $parent);
		return $result;
	}
	
	/*
	Xuất danh sách menu ra dạng array(key => value)
	*/
	function get_menu_array($parent = 0, $type = '', $pre_fix = '') {
		$result = $this->get_menu($parent, $type);
		return $this->get_recursive_array($result, $parent, $pre_fix);
	}
	
	function get_recursive_array($allmenu, $parent = 0, $pre_fix, $tpre_fix = '', &$rmenu = array()) {
		$tpre_fix = $tpre_fix . $pre_fix;
		foreach ($allmenu as $menu) {
			if ($menu->term_parent == $parent) $tpre_fix = '';
			$rmenu[$menu->term_id] = $tpre_fix . $menu->term_name;
			if (isset($menu->child) && $menu->child) {
				$this->get_recursive_array($menu->child, $parent, $pre_fix, $tpre_fix, $rmenu);
			}
		}
		return $rmenu;
	}
	
	
	function get_menu_slug_array($parent = 0, $type = '', $pre_fix = '') {
		$result = $this->get_menu($parent, $type);
		return $this->get_recursive_slug_array($result, $parent,$pre_fix);
	}
	
	function get_recursive_slug_array($allmenu, $parent = 0, $pre_fix ='/', &$menu_id_slug = array(), &$tpre_fix='') {
		foreach ($allmenu as $menu) {
			if ($menu->term_parent == $parent) $tpre_fix = '';

			if($menu->term_parent !=0)
			{
				if(isset($menu_id_slug[$menu->term_parent]) && $menu_id_slug[$menu->term_parent] !='')
					$tpre_fix = $menu_id_slug[$menu->term_parent];
			}

			$tpre_fix = $tpre_fix.$menu->term_slug.$pre_fix;
			$menu_id_slug[$menu->term_id] = $tpre_fix;

			if (isset($menu->child) && $menu->child) {
				$this->get_recursive_slug_array($menu->child, $parent,$pre_fix, $menu_id_slug, $tpre_fix);
			}
		}
		return $menu_id_slug;
	}
	
	
	function get_recursive(&$allmenu, $parent = 0) {
		$id_field = 'term_id';
		$parent_field = 'term_parent';
		
		//get current child
		$menu = array();
		if ($allmenu && is_array($allmenu)) foreach ($allmenu as $k => $v) {
			if ($v->{$parent_field} == $parent) {
				$menu[] = $v;
				unset($allmenu[$k]);
			}
		}
		if ($menu) foreach ($menu as & $item) {
			$temp_menu = $this->get_recursive($allmenu, $item->{$id_field});
			if ($temp_menu) {
				$item->child = $temp_menu;
			}
		}
		return $menu;
	}
	
	public function update_recursive($menus, $parent = 0) {
		$data_update = array();
		$order = 1;
		if ($menus) foreach ($menus as $menu) {
			if (isset($menu['id']) && $menu['id']) {
				$data_update['term_parent'] = $parent;
				$data_update['term_order'] = $order++;
				$this->update($menu['id'], $data_update);
				
				// echo 'updating ' .$menu['id']. '=>'.$parent ."\n";
				
			}
			if (isset($menu['children']) && $menu['children']) {
				$this->update_recursive($menu['children'], $menu['id']);
			}
		}
	}
	
	function get_the_terms($post_id = 0, $taxonomy = 'category') {
		if (!$post_id) return false;
		if ($this->taxonomy_exists($taxonomy)) return false;
		$this->db->join('term_posts', 'term_posts.term_id = term.term_id', 'left');
		$this->db->where('term.term_type',$taxonomy);
		$this->db->where('post_id', $post_id);
		return $this->get_many_by();
	}

	/**
	* Checks that the taxonomy name exists.
	*
	* Formerly is_taxonomy()
	*
	* @uses $wp_taxonomies
	*
	* @param string $taxonomy Name of taxonomy object
	* @return bool Whether the taxonomy exists.
	*/
	function taxonomy_exists( $taxonomy ) {
		return $this->get_by('term_name', $taxonomy);
	}
	
	/**
	* Tạo file cache tạm
	*/
	function build_link_cache($term_type = ''){
		if(in_array($term_type, array('category','event')))
		{
			$m_name = $term_type.'_term_model';
			$this->load->model($m_name,$term_type);
			$this->$term_type->build_link_cache();
		}
	}
}
?>